using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlTypes;
using MySql.Data.MySqlClient;

namespace FutureOpen.Core.func.sql
{
    public class IndexActionProvider
    {
        public static int INDEX_ACTION_INSERT = 0;
        public static int INDEX_ACTION_DELETE = 1;
        public static int INDEX_ACTION_MODIFY = 2;

        public static readonly string SQL_ADD_IACTION = "insert into iaction(iaction_table,iaction_target_id,iaction_action,iaction_time) Values('{0}',{1},{2},'{3}')";
        public static void AddIndexAction(string table, int id, int action)
        {
            string sql = string.Format(SQL_ADD_IACTION, table, id, action, DateTime.Now);
            MySqlHelper.ExecuteNonQuery(MySqlHelper.CONNSTR(), sql, null);
        }

        public static readonly string SQL_SELECT_IACTION = "select iaction_id,iaction_table,iaction_target_id,iaction_action,iaction_time from iaction order by iaction_table,iaction_target_id";
        public static readonly string SQL_SELECT_IACTION_LIMIT = "select iaction_id,iaction_table,iaction_target_id,iaction_action,iaction_time from iaction order by iaction_table,iaction_target_id limit {0}";
        public static DataSet SelectIndexAction(int count)
        {
            string sql;
            if (count < 0)
                sql = SQL_SELECT_IACTION;
            else if (count > 0)
                sql = string.Format(SQL_SELECT_IACTION_LIMIT, count);
            else 
                return null;
            return MySqlHelper.ExecuteDataset(MySqlHelper.CONNSTR(), sql, null);
        }

        public static readonly string SQL_DELETE_IACTION = "delete from iaction where iaction_id >= {0} and iaction_id <= {1}";
        public static void RemoveIndexAction(int start, int end)
        {
            string sql = string.Format(SQL_DELETE_IACTION, start, end);
            MySqlHelper.ExecuteNonQuery(MySqlHelper.CONNSTR(), sql, null);
        }

        public static readonly string SQL_SELECT_DATA = "select * from {0} where {1} = {2}";
        public static DataRow SelectDataRow(string tablename, string pkey,int id)
        {
            string sql = string.Format(SQL_SELECT_DATA, tablename, pkey, id);
            return MySqlHelper.ExecuteDatarow(MySqlHelper.CONNSTR(), sql, null);
        }
        public static DataSet SelectDataSet(string tablename, string pkey, int id)
        {
            string sql = string.Format(SQL_SELECT_DATA, tablename, pkey, id);
            return MySqlHelper.ExecuteDataset(MySqlHelper.CONNSTR(), sql, null);
        }
    }
}
